package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/setmeal")

public class SetMealController {

    @Autowired
    private SetMealService setMealService;
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", key = "'setmeal_'+#result.id")
    public Result add(@RequestBody SetmealDTO setMealDTO) {
        setMealService.add(setMealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setMealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    @Cacheable(cacheNames = "setmealCache", key = "'setmeal_'+#id")
    public Result getById(@PathVariable("id") Integer id) {
        SetmealDTO setmealDTO = setMealService.getById(id);
        return Result.success(setmealDTO);
    }

    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        setMealService.update(setmealDTO);
        return Result.success();
    }

    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result delByIds(@RequestParam Integer[] ids) {
        setMealService.delete(ids);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result updateStatus(@RequestParam Integer id, @PathVariable("status") Integer status) {
        setMealService.updateStatus(id, status);
        return Result.success();
    }
}
